package com.company.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.AActiveEntity;
import com.company.project.entity.AAttendRecordEntity;
import com.company.project.entity.AClassEntity;
import com.company.project.entity.APaidRecordEntity;
import com.company.project.service.AActiveService;
import com.company.project.service.AAttendRecordService;
import com.company.project.service.AClassService;
import com.company.project.service.APaidRecordService;
import com.company.project.vo.req.ClassActiveReqVo;
import com.company.project.vo.resp.ActivePaidVo;
import com.company.project.vo.resp.ClassActiveVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/")
public class AReportController {
    @Autowired
    private AClassService aClassService;
    @Autowired
    private AActiveService aActiveService;
    @Autowired
    private APaidRecordService aPaidRecordService;
    @Autowired
    private AAttendRecordService aAttendRecordService;

    /**
     * 跳转到班级活动数统计页面
     */
    @GetMapping("/index/classActive")
    public String classActive() {
        return "report/classActive/list";
    }

    /**
     * 跳转到活动报名人数统计页面
     */
    @GetMapping("/index/activePaid")
    public String activePaid() {
        return "report/activePaid/list";
    }

    @ApiOperation(value = "班级活动数量统计")
    @PostMapping("report/classActive/listByPage")
    @RequiresPermissions("report:classActive:list")
    @ResponseBody
    public DataResult findClassActiveListByPage(@RequestBody ClassActiveReqVo reqVo){
        LambdaQueryWrapper<AClassEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件
        queryWrapper.like(StringUtils.isNotBlank(reqVo.getName()),AClassEntity::getName, reqVo.getName());
        List<AClassEntity> list = aClassService.list(queryWrapper);
        //对查询的班级进行循环查询  关联活动数量
        List<ClassActiveVo> voList = new ArrayList<>();
        list.forEach(e ->{
            int count = aActiveService.lambdaQuery().eq(AActiveEntity::getClassId,e.getId()).count();
            ClassActiveVo vo = new ClassActiveVo();
            vo.setClassName(e.getName());
            vo.setActiveNumber(count);
            voList.add(vo);
        });
        return DataResult.success(voList);
    }

    @ApiOperation(value = "活动人数金额统计")
    @PostMapping("report/activePaid/listByPage")
    @RequiresPermissions("report:activePaid:list")
    @ResponseBody
    public DataResult findActivePaidListByPage(@RequestBody ClassActiveReqVo reqVo){
        LambdaQueryWrapper<AActiveEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件
        queryWrapper.like(StringUtils.isNotBlank(reqVo.getName()),AActiveEntity::getName, reqVo.getName());
        List<AActiveEntity> list = aActiveService.list(queryWrapper);
        List<ActivePaidVo> voList = new ArrayList<>();
        list.forEach(e ->{
            //总报名人数
            int paidNum = aPaidRecordService.lambdaQuery()
                    .eq(APaidRecordEntity::getActiveId,e.getId())
                    .count();
            //总签到人数
            int attendNum = aAttendRecordService.lambdaQuery()
                    .eq(AAttendRecordEntity::getActiveId,e.getId())
                    .count();
            //总费用
            BigDecimal totalAmount = aPaidRecordService.sumPaidAmountByActiveId(e.getId());
            ActivePaidVo vo = new ActivePaidVo();
            vo.setActiveName(e.getName());
            vo.setClassName(e.getClassName());
            vo.setPaidNum(paidNum);
            vo.setAttendNum(attendNum);
            vo.setTotalAmount(totalAmount);
            voList.add(vo);
        });
        return DataResult.success(voList);
    }
}
